
var Ghost = function () {

	this.mode = "normal";

};

var p = Ghost.prototype = new MazeCharacter();

p.move = function () {

	var px = this._x;
	var py = this._y;

	var pacMan = this.pacMan;

	var dx = px - pacMan._x;
	var dy = py - pacMan._y;

	if ( dx * dx + dy * dy < 64 ) {

		this.onColidePacMan();

	}

	if ( px % 8 == 0 && py % 8 == 0 ) {

		var rx = px >> 3;
		var ry = py >> 3;

		if ( !( ry == 14 && ( rx < 6 || rx > 22 ) ) ) {

			this._alpha = 100;

			this.AI.getDirection();

		} else { this._alpha = 50; };

	}

	px += this.vx;
	py += this.vy;

	if ( px > 240 ) { px = -24 } else if ( px < -24 ) { px = 240 };

	this._x = px;
	this._y = py;

	this.updateAnimation();

}

p.invertDirection = function () {

	switch ( this.current_dir ) {

		case 1: this.current_dir = 2; break;
		case 2: this.current_dir = 1; break;
		case 4: this.current_dir = 8; break;
		case 8: this.current_dir = 4; break;

	}

}

p.getVector = function () {

	switch ( this.current_dir ) {

		case 1: this.vx = 2, this.vy = 0; break;
		case 2: this.vx =-2, this.vy = 0; break;
		case 4: this.vx = 0, this.vy = 2; break;
		case 8: this.vx = 0, this.vy =-2; break;

	}

}

p.updateAnimation = function () {

	if ( this.mode == "scared" ) { this.gotoAndStop ( "scared" ); this.animation.nextFrame(); return; }

	     if ( this.vx ==  2 ) { this.gotoAndStop ( this.mode + "_dir1" ); this.animation.nextFrame(); }
	else if ( this.vx == -2 ) { this.gotoAndStop ( this.mode + "_dir2" ); this.animation.nextFrame(); }
	else if ( this.vy ==  2 ) { this.gotoAndStop ( this.mode + "_dir4" ); this.animation.nextFrame(); }
	else if ( this.vy == -2 ) { this.gotoAndStop ( this.mode + "_dir8" ); this.animation.nextFrame(); }
	else this.animation.stop();

}

p.switchToPelletMode = function () {

	clearInterval ( this.onBlinkTimeout );
	clearInterval ( this.blinkInterval );

	this.switchIntervalMS( 33 );

	this.mode = "scared";

	this.invertDirection();

	this.AI = new RandomAI( this );

	this.onColidePacMan = this.onKillGhost;

	this.onBlinkTimeout = setInterval ( this , "onBlink" , 4000 );

}

p.switchToNormalMode = function ( global ) {

	if ( global && this.mode == "eye" ) return;

	clearInterval ( this.blinkInterval );
	clearInterval ( this.onBlinkTimeout );

	this.AI = new SimpleFollowAI ( this );

	this.switchIntervalMS( 26 );

	this.mode = "normal";

	this.bitflag = 1;

	this.onColidePacMan = this.onKillPacMan;

}

p.onKillPacMan = function () {

	trace ( "kill PacMan" );

}

p.onKillGhost = function () {

	delete this.onColidePacMan;

	clearInterval ( this.onBlinkTimeout );
	clearInterval ( this.blinkInterval );
	clearInterval( this.pelletTimeout );

	var snd = new Sound ( this );

	snd.attachSound ( "snd_killGhost" );

	snd.start();

	this.mode = "eye";

	this.switchIntervalMS ( 16 );

	this.target = { _x: ( ( random(2) == 0 ) ? 13 : 14 ) << 3, _y: 13 << 3 };

	this.bitflag = 2;

	this.AI = new SimpleFollowAI ( this );

}

p.onBlink = function () {

	clearInterval ( this.onBlinkTimeout );

	this.blinkInterval = setInterval ( this , "blink" , 256 );

}

p.blink = function () {

	this.mode = ( this.mode == "scared" ) ? "normal" : "scared";

}

var Ghost_Red = function () {

	this.AI = new PathFollowAI( this , [2,2,2,2,2,4,4,4,2,2,2,8,8,8,8,8,8,8,8,8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,8,8,8,8,1,1,1,1,1,4,4,4,4,2,2,1,1,"end"] );

	this.target = this.pacMan;

	this.onColidePacMan = this.onKillPacMan;

	this.bitflag = 1;

};

var p = Ghost_Red.prototype = new Ghost();

